import {createRouter, createWebHashHistory} from "vue-router";

const routes = [
    {
        path: "/",
        redirect: "Home",
    },
    {
        path: "/Home",
        name: "Home",
        component: () => import('../Views/Home.vue'),
        redirect: '/ru',
        children: [
            {
                path: "/ru",
                name: "ru",
                component: () => import('../Views/culture.vue'),
                meta: {
                    title: "儒家文化展厅",
                    color: '#71361D'
                }
            },
            {
                path: "/qi",
                name: "qi",
                component: () => import('../Views/culture.vue'),
                meta: {
                    title: "齐文化展厅",
                    color: '#B78B26'
                }
            },
            {
                path: "/huang",
                name: "huang",
                component: () => import('../Views/culture.vue'),
                meta: {
                    title: "黄河文化展厅",
                    color: '#DE9200'
                }
            },
            {
                path: "/hong",
                name: "hong",
                component: () => import('../Views/culture.vue'),
                meta: {
                    title: "红色文化展厅",
                    color: '#C02C38'
                }
            },{
                path: "/Character",
                name: "Character",
                component: () => import('../Views/Character.vue'),
                meta: {
                    title: "人物展厅",
                    color: '#533F31'
                }
            },
            {
                path: "/Suggestion",
                name: "Suggestion",
                meta: {
                    title: '意见反馈',
                    color: '#835426',
                    requiresAuth: true, // 路由拦截，未登录不能使用
                },
                component: () => import('../Views/Suggestion.vue'),

            },
            {
                path: "/User",
                name: "User",
                meta: {
                    title: '用户信息',
                    color: '#126E82',
                    requiresAuth: true, // 路由拦截，未登录不能使用
                },
                component: () => import('../Views/My.vue'),

            }
        ]
    },

    {
        path: "/Login",
        name: "Login",
        component: () => import('../Views/Login.vue')
    },
];


const router = createRouter({
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
});

// 路由拦截，判断是否需要登录
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        //判断是否有标题
        document.title = to.meta.title;
    }
    window.scrollTo(0, 0);

    // console.log("title",document.title)
    // 通过requiresAuth判断当前路由导航是否需要登录
    if (to.matched.some(record => record.meta.requiresAuth)) {
        let token = sessionStorage.getItem('username')
        // console.log("token",token)
        // 若需要登录访问，检查是否为登录状态
        if (!token) {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        } else {
            next()
        }
    } else {
        next() // 确保一定要调用 next()
    }
})
export default router;
